#include "circ.h"
#include "string.h"


void cb_init(circular_buffer *cb, char *buf, size_t capacity, size_t sz)
{
    cb->buffer = buf;
    cb->buffer_end = (char *)cb->buffer + capacity * sz;
    cb->capacity = capacity;
    cb->count = 0;
    cb->sz = sz;
    cb->head = cb->buffer;
    cb->tail = cb->buffer;
}

void cb_push_back(circular_buffer *cb, const void *item)
{
    memcpy(cb->head, item, cb->sz);
    cb->head = (char*)cb->head + cb->sz;
    if(cb->head == cb->buffer_end)
      cb->head = cb->buffer;
    
    if(cb->count < cb->capacity)
      cb->count++;
    else
       cb->tail == cb->head;
}

void cb_pop_front(circular_buffer *cb, void *item)
{
  if(cb->count > 0)
  {
    short *test;
    memcpy(item, cb->tail, cb->sz);
    cb->tail = (char*)cb->tail + cb->sz;
    if(cb->tail == cb->buffer_end)
        cb->tail = cb->buffer;
    cb->count--;
  }
}